package pogvue.gui.popups;

import pogvue.gui.AlignViewport;
import pogvue.gui.Controller;
import pogvue.gui.event.AlignViewportEvent;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public final class PIDWindowPopup extends Popup implements ChangeListener {
  private final JLabel tfLabel;
  private final JSlider slider;

  public PIDWindowPopup(JPanel parent,AlignViewport av,Controller c,String title) {
    super(parent,av,c,title);

    System.out.println("Window set to " + av.getPIDWindow());

    tfLabel = new JLabel("Window size : ");
    slider  = new JSlider(JSlider.HORIZONTAL,0,100,av.getPIDWindow());
    slider.setMajorTickSpacing(10);
    slider.setMinorTickSpacing(5);
    slider.setPaintTicks(true);
    slider.setPaintLabels(true);
    slider.setSnapToTicks(true);

    slider.addChangeListener(this);

    gbc.fill = GridBagConstraints.NONE;

    gbc.insets = new Insets(20,20,20,20);

    add(tfLabel,gb,gbc,0,0,1,1);
    add(slider,gb,gbc,1,0,4,1);

//    add(apply,gb,gbc,0,3,1,1);
//    add(close,gb,gbc,1,3,1,1);

    pack();
    show();
  }

  public void stateChanged(ChangeEvent e) {
     int value = slider.getValue();

     value++;

     if (value != av.getPIDWindow()) {
       if (value > 100) {
         value = 100;
       } 
       av.setPIDWindow(value);
       controller.handleAlignViewportEvent(new AlignViewportEvent(this,av,AlignViewportEvent.SHOW));
     }
  } 
}
